package com.fingard.dsp.bank.directbank.abc01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 中国农业银行银企直联专线版(ABC01)余额查询
 */
public class ABC01Balance extends ABC01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();

        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();

        try {
            while (p_req7506.readRecord()) {
                ABC01Header bankHeader = new ABC01Header();
                bankHeader.reqSeqID = p_req7506.reqHeader.transSeqID;
                bankHeader.transCode = ABC01TransCode.deal_7506;

                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHead(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Cmp");
                xmlWriter.writeElementString("DbProv", p_req7506.readedDetail.actArea);
                xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req7506.readedDetail.actNum));
                xmlWriter.writeElementString("DbCur", convertCurToBank(p_req7506.readedDetail.actCur));
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRespInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                        if (StringHelper.isNullOrEmpty(tmpRespInfo)) {
                            tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                        }

                        String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                        if ("0000".equals(tmpRespCode)) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpRespInfo;

                            if (tmpBkReader.read("/ap/Acc")) {
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                                tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Bal");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("FrzAmt");
                                tmpRetDetail.yesterdayBal = tmpBkReader.readedXElement.getSingleText("LastBal");
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpRespInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            }
            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}